
//  AFFManger.m
//  CasualPay
//
//  Created by LiuYongfu on 15/10/21.
//
//

#import "AFFManger.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"
#import "BaseViewController.h"
#import "BaseTabBarController.h"
#import "ZBAppDelegate.h"
#import "SLBJSONModels.h"

@interface AFFManger ()


@end


@implementation AFFManger

#pragma mark - 数据加载的菊花
//显示hud的通用方法

+(void)showLoading {
	[CustomMBHUD showHUDLoadingWithTitle:@"网络加载中..."];
}


+ (void)hideLoading {
	[CustomMBHUD hide];
}



+ (NSURLSessionDownloadTask *)downloadFileWithOption:(NSDictionary *)paramDic requestURL:(NSString*)requestURL savedPath:(NSString*)savedPath progress:(void (^)(NSProgress *progress))progress downloadSuccess:(void (^)(id responseObject))success downloadFailure:(void (^)(NSError *error))failure
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    // 1. 创建会话管理者
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    // 2. 创建下载路径和请求对象 http://dldir1.qq.com/qqfile/QQforMac/QQ_V5.4.0.dmg
    NSURL *URL = [NSURL URLWithString:requestURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    // 3.创建下载任务
    /**
     * 第一个参数 - request：请求对象
     * 第二个参数 - progress：下载进度block
     *      其中： downloadProgress.completedUnitCount：已经完成的大小
     *            downloadProgress.totalUnitCount：文件的总大小
     * 第三个参数 - destination：自动完成文件剪切操作
     *      其中： 返回值:该文件应该被剪切到哪里
     *            targetPath：临时路径 tmp NSURL
     *            response：响应头
     * 第四个参数 - completionHandler：下载完成回调
     *      其中： filePath：真实路径 == 第三个参数的返回值
     *            error:错误信息
     */
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress) {
        progress(downloadProgress);
        
    } destination:^(NSURL *targetPath, NSURLResponse *response) {
        NSURL *path = [NSURL fileURLWithPath:savedPath];
        return path;        
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        NSLog(@"File downloaded to: %@", filePath);
        if (error) {
            failure(error);
        } else {
            success(filePath);
        }
    }];
    // 4. 开启下载任务
    [downloadTask resume];
    return downloadTask;
}

#pragma mark - 最常用的GET和POST

+ (void)getDataWithAPI:(NSString *)apiName
         andArrayParam:(NSArray *)arrayParam
          andDictParam:(NSDictionary *)dictParam
             dataModel:(NSString *)modelName
         isShowLoading:(BOOL)isShowLoading
      requestSuccessed:(RequestSuccessed)requestSuccessed
        requestFailure:(RequestFailure)requestFailure{
    NSString *url = KBasePathUrl;
    [self requestByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:nil dataModel:modelName requestType:RequestTypeGET isShowLoading:isShowLoading requestSuccessed:requestSuccessed requestFailure:requestFailure];
}

+ (void)getDataWithAPI:(NSString *)apiName
		 andArrayParam:(NSArray *)arrayParam
		  andDictParam:(NSDictionary *)dictParam
		dataModel:(NSString *)modelName
		 isShowLoading:(BOOL)isShowLoading
		requestSuccessed:(RequestSuccessed)requestSuccessed
		requestFailure:(RequestFailure)requestFailure
		sender: (id)Sender{
	NSString *url = KBasePathUrl;
	UIColor * org_bg_color;
	UIView * view;
	if (Sender) {
		if ([Sender isKindOfClass:[UIView class]]) {
			view = (UIView*)Sender;
			org_bg_color = view.backgroundColor;
			[view setUserInteractionEnabled:NO];
			[view setBackgroundColor:BG_GRAY_H];
		}
	}
	[self requestByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:nil dataModel:modelName requestType:RequestTypeGET isShowLoading:isShowLoading requestSuccessed:^(NSString *message, id responseObject) {
		requestSuccessed(message,responseObject);
		if (view) {
			[view setUserInteractionEnabled:YES];
			if (org_bg_color) {
				[view setBackgroundColor:org_bg_color];
			}
		}
	} requestFailure:^(NSInteger errorCode, NSString *errorMessage) {
		requestFailure(errorCode,errorMessage);
		if (view) {
			[view setUserInteractionEnabled:YES];
			if (org_bg_color) {
				[view setBackgroundColor:org_bg_color];
			}
		}
	}];

}



+ (void)postDataWithAPI:(NSString *)apiName
          andArrayParam:(NSArray *)arrayParam
           andDictParam:(NSDictionary *)dictParam
              dataModel:(NSString *)modelName
          isShowLoading:(BOOL)isShowLoading
       requestSuccessed:(RequestSuccessed)requestSuccessed
         requestFailure:(RequestFailure)requestFailure{
    NSString *url = KBasePathUrl;
    
    NSArray *keys = [dictParam allKeys];
    NSMutableDictionary *dicTemp = [NSMutableDictionary dictionaryWithDictionary:dictParam];
    
    if (![keys containsObject:@"userId"]) {
        NSString *userId = [[NSUserDefaults standardUserDefaults] valueForKey:@"userId"];
        if (![NSString isNilOrEmpty:userId]) {
            [dicTemp setObject:userId forKey:@"userId"];
        }
    }
    [self requestByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dicTemp andBodyParam:nil dataModel:modelName requestType:RequestTypePOST isShowLoading:isShowLoading requestSuccessed:requestSuccessed requestFailure:requestFailure];
    
    
}

+ (void)postDataWithAPISub:(NSString *)apiName
          andArrayParam:(NSArray *)arrayParam
           andDictParam:(NSDictionary *)dictParam
              dataModel:(NSString *)modelName
          isShowLoading:(BOOL)isShowLoading
       requestSuccessed:(RequestSuccessed)requestSuccessed
         requestFailure:(RequestFailure)requestFailure{
    NSString *url = KBasePathUrl;
    [self requestByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:nil dataModel:modelName requestType:RequestTypePOST isShowLoading:isShowLoading requestSuccessed:requestSuccessed requestFailure:requestFailure];
}


+ (void)postDataWithAPI:(NSString *)apiName
		  andArrayParam:(NSArray *)arrayParam
		   andDictParam:(NSDictionary *)dictParam
			  dataModel:(NSString *)modelName
		  isShowLoading:(BOOL)isShowLoading
	   requestSuccessed:(RequestSuccessed)requestSuccessed
		 requestFailure:(RequestFailure)requestFailure
	    sender:(id)Sender{
	NSString *url = KBasePathUrl;
	UIColor * org_bg_color;
	UIView * view;
	if (Sender) {
		if ([Sender isKindOfClass:[UIView class]]) {
			view = (UIView*)Sender;
			org_bg_color = view.backgroundColor;
			[view setUserInteractionEnabled:NO];
			[view setBackgroundColor:BG_GRAY_H];
		}
	}
	[self requestByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:nil dataModel:modelName requestType:RequestTypePOST isShowLoading:isShowLoading requestSuccessed:^(NSString *message, id responseObject) {
		requestSuccessed(message,responseObject);
		if (view) {
			[view setUserInteractionEnabled:YES];
			if (org_bg_color) {
				[view setBackgroundColor:org_bg_color];
			}
		}
	} requestFailure:^(NSInteger errorCode, NSString *errorMessage) {
		requestFailure(errorCode,errorMessage);
		if (view) {
			[view setUserInteractionEnabled:YES];
			if (org_bg_color) {
				[view setBackgroundColor:org_bg_color];
			}
		}
	}];

}

+ (void)postBodyDataWithAPI:(NSString *)apiName
              andArrayParam:(NSArray *)arrayParam
               andDictParam:(NSDictionary *)dictParam
               andBodyParam:(NSString *)bodyParam
                  dataModel:(NSString *)modelName
              isShowLoading:(BOOL)isShowLoading
           requestSuccessed:(RequestSuccessed)requestSuccessed
             requestFailure:(RequestFailure)requestFailure {
    NSString *url = KBasePathUrl;
    [self requestByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:bodyParam dataModel:modelName requestType:RequestTypePostBodyData isShowLoading:isShowLoading requestSuccessed:requestSuccessed requestFailure:requestFailure];
}

#pragma mark - 自定义url前缀的GET和POST

+ (void)getDataToUrl:(NSString *)url
               withAPI:(NSString *)apiName
         andArrayParam:(NSArray *)arrayParam
          andDictParam:(NSDictionary *)dictParam
             dataModel:(NSString *)modelName
         isShowLoading:(BOOL)isShowLoading
      requestSuccessed:(RequestSuccessed)requestSuccessed
        requestFailure:(RequestFailure)requestFailure{
    [self requestByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:nil dataModel:modelName requestType:RequestTypeGET isShowLoading:isShowLoading requestSuccessed:requestSuccessed requestFailure:requestFailure];
}

+ (void)postDataToUrl:(NSString *)url
              withAPI:(NSString *)apiName
        andArrayParam:(NSArray *)arrayParam
         andDictParam:(NSDictionary *)dictParam
            dataModel:(NSString *)modelName
        isShowLoading:(BOOL)isShowLoading
     requestSuccessed:(RequestSuccessed)requestSuccessed
       requestFailure:(RequestFailure)requestFailure {
    [self requestByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:nil dataModel:modelName requestType:RequestTypePOST isShowLoading:isShowLoading requestSuccessed:requestSuccessed requestFailure:requestFailure];
}

+ (void)postBodyDataToUrl:(NSString *)url
                  withAPI:(NSString *)apiName
            andArrayParam:(NSArray *)arrayParam
             andDictParam:(NSDictionary *)dictParam
             andBodyParam:(NSString *)bodyParam
                dataModel:(NSString *)modelName
            isShowLoading:(BOOL)isShowLoading
         requestSuccessed:(RequestSuccessed)requestSuccessed
           requestFailure:(RequestFailure)requestFailure {
    [self requestByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:bodyParam dataModel:modelName requestType:RequestTypePostBodyData isShowLoading:isShowLoading requestSuccessed:requestSuccessed requestFailure:requestFailure];
}

#pragma mark - 通用的GET和POST（只返回BaseModel的Data内容）

/**
 *  发起get & post网络请求
 *
 *  @param url              接口前缀 最后的'/'可有可无
 *  @param apiName          方法名称 前面不能有'/'
 *  @param arrayParam       数组参数，用来组装url/param1/param2/param3，参数的顺序很重要
 *  @param dictParam        字典参数，key-value
 *  @param modelName        模型名称字符串
 *  @param requestType      RequestTypeGET 和 RequestTypePOST
 *  @param requestSuccessed 请求成功的回调
 *  @param requestFailure   请求失败的回调
 */
+ (void)requestByUrl:(NSString *)url
             withAPI:(NSString *)apiName
       andArrayParam:(NSArray *)arrayParam
        andDictParam:(NSDictionary *)dictParam
        andBodyParam:(NSString *)bodyParam
           dataModel:(NSString *)modelName
         requestType:(RequestType)requestType
       isShowLoading:(BOOL)isShowLoading
    requestSuccessed:(RequestSuccessed)requestSuccessed
      requestFailure:(RequestFailure)requestFailure {
    [self   requestByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:bodyParam imageData:nil requestType:requestType
       isShowLoading:isShowLoading  requestSuccessed: ^(NSString * message,id responseObject) {
            BaseModel *baseModel = (BaseModel *)responseObject;
    
            if (baseModel.resultCode == 200) {  //接口访问成功
                NSObject *dataModel = baseModel.resultData;
                JSONModelError *initError = nil;
                if ([dataModel isKindOfClass:[NSArray class]]) {
                    if ( [modelName length] > 0 && [NSClassFromString(modelName) isSubclassOfClass:[CommonBaseModel class]]) {
                        dataModel = [NSClassFromString(modelName) arrayOfModelsFromDictionaries:(NSArray *)dataModel error:&initError];
                    }
                }
                else if ([dataModel isKindOfClass:[NSDictionary class]]) {
                    if ( [modelName length] > 0 && [NSClassFromString(modelName) isSubclassOfClass:[CommonBaseModel class]]) {
                        dataModel = [[NSClassFromString(modelName) alloc] initWithDictionary:(NSDictionary *)dataModel error:&initError];
                    }
                }
                
                //针对转换映射后的处理
                if (initError) {
                    requestFailure(1101, initError.localizedDescription);
                } else {

                    requestSuccessed(@"获取数据成功",dataModel);
                }
            } else {
                requestFailure(baseModel.resultCode,baseModel.msg);
            }
        } requestFailure:requestFailure];
}

#pragma mark - 通用的GET和POST（返回BaseModel的所有内容）

/**
 *  发起get & post & 上传图片 请求
 *
 *  @param url              接口前缀 最后的'/'可有可无
 *  @param apiName          方法名称 前面不能有'/'
 *  @param arrayParam       数组参数，用来组装url/param1/param2/param3，参数的顺序很重要
 *  @param dictParam        字典参数，key-value
 *  @param imageData        图片资源
 *  @param requestType      RequestTypeGET 和 RequestTypePOST
 *  @param requestSuccessed 请求成功的回调
 *  @param requestFailure   请求失败的回调
 */
+ (void)requestByUrl:(NSString *)url
             withAPI:(NSString *)apiName
       andArrayParam:(NSArray *)arrayParam
        andDictParam:(NSDictionary *)dictParam
        andBodyParam:(NSString *)bodyParam
           imageData:(NSData *)imageData
         requestType:(RequestType)requestType
       isShowLoading:(BOOL)isShowLoading
    requestSuccessed:(RequestSuccessed)requestSuccessed
      requestFailure:(RequestFailure)requestFailure {
        [self requestDataByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:bodyParam imageData:imageData isNeedModel:YES requestType:requestType isShowLoading:isShowLoading requestSuccessed:requestSuccessed requestFailure:requestFailure];
}


#pragma mark - 通用的GET、POST 和 上传图片（不用数据模型返回字典）
/**
 *  通过字典返回时候数据
 *
 *  @param url              <#url description#>
 *  @param apiName          <#apiName description#>
 *  @param arrayParam       <#arrayParam description#>
 *  @param dictParam        <#dictParam description#>
 *  @param bodyParam        <#bodyParam description#>
 *  @param imageData        <#imageData description#>
 *  @param requestType      <#requestType description#>
 *  @param requestSuccessed <#requestSuccessed description#>
 *  @param requestFailure   <#requestFailure description#>
 */
+ (void)requestDataForDicByUrl:(NSString *)url
                       withAPI:(NSString *)apiName
                 andArrayParam:(NSArray *)arrayParam
                  andDictParam:(NSDictionary *)dictParam
                  andBodyParam:(NSString *)bodyParam
                     imageData:(NSData *)imageData
                   requestType:(RequestType)requestType
                 isShowLoading:(BOOL)isShowLoading
              requestSuccessed:(RequestSuccessed)requestSuccessed
                requestFailure:(RequestFailure)requestFailure;
{
    [self requestDataByUrl:url withAPI:apiName andArrayParam:arrayParam andDictParam:dictParam andBodyParam:bodyParam imageData:imageData isNeedModel:NO requestType:requestType  isShowLoading:isShowLoading requestSuccessed:requestSuccessed requestFailure:requestFailure];
}




#pragma mark - 私有的请求方法
/**
 *  封装的网络请求方法
 *
 *  @param url              <#url description#>
 *  @param apiName          <#apiName description#>
 *  @param arrayParam       <#arrayParam description#>
 *  @param dictParam        <#dictParam description#>
 *  @param bodyParam        <#bodyParam description#>
 *  @param imageData        <#imageData description#>
 *  @param isNeedModel      <#isNeedModel description#>
 *  @param requestType      <#requestType description#>
 *  @param requestSuccessed <#requestSuccessed description#>
 *  @param requestFailure   <#requestFailure description#>
 */
+ (void)requestDataByUrl:(NSString *)url
              withAPI:(NSString *)apiName
        andArrayParam:(NSArray *)arrayParam
         andDictParam:(NSDictionary *)dictParam
         andBodyParam:(NSString *)bodyParam
            imageData:(NSData *)imageData
             isNeedModel:(BOOL)isNeedModel
          requestType:(RequestType)requestType
           isShowLoading:(BOOL)isShowLoading
     requestSuccessed:(RequestSuccessed)requestSuccessed
       requestFailure:(RequestFailure)requestFailure {
    
    if (isShowLoading) {
        [self showLoading];
    }

    //1. url合法性判断
    if (![NSURL URLWithString:url]) {
        requestFailure(1005, [NSString stringWithFormat:@"传递的url[%@]不合法！", url]);
        return;
    }

    //3. 组装完整的url地址
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 60;
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json; charset=UTF-8",@"application/octet-stream",@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain",@"text/html;charset=utf-8",nil];
    
    NSString *urlString  = url;
    if (apiName) {
        urlString = [url stringByAppendingFormat:@"%@%@",
                     ([url hasSuffix:@"/"] ? @"" : @"/"),     //确保url前缀后面有1个字符'/'
                     ([apiName hasPrefix:@"/"] ? [apiName substringFromIndex:1] : apiName)   //确保apiName前面没有字符'/'
                     ];                                                         //组装后的完整url地址
    }
    
    
    //5. 组装数组参数
    NSMutableString *newUrlString = [NSMutableString stringWithString:urlString];
    for (NSObject *param in arrayParam) {
        [newUrlString appendString:@"/"];
        [newUrlString appendFormat:@"%@",param];
    }
    
    
    NSMutableDictionary *dicTemp = [NSMutableDictionary dictionaryWithDictionary:dictParam];
    
    
    NSArray *keys = [dictParam allKeys];
    NSString *strContent = nil;
    if ([keys containsObject:@"content"]) {
        strContent = [dictParam objectForKey:@"content"];
        [dicTemp removeObjectForKey:@"content"];
    }else if([keys containsObject:@"contentNew"]){
        NSString * content = [dictParam objectForKey:@"contentNew"];
        [dicTemp removeObjectForKey:@"contentNew"];
        [dicTemp setObject:content forKey:@"content"];
    }
    

    //AES加密转16进制
    NSString *aesStr = [[dicTemp JSONString] stringWithAES_HEXBytes];
    //时间戳
    NSNumber *numberTime = [NSNumber numberWithLongLong:[NSDate timeMicro]];
    //sha1加密
    NSString *sha1 = [[NSString stringWithFormat:@"%@%@",[numberTime stringValue],ValidateKey] sha1];
    if (aesStr) {
        sha1 = [[NSString stringWithFormat:@"%@%@%@",[numberTime stringValue],ValidateKey,aesStr] sha1];
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"timestamp"] = numberTime;
    dic[@"sha1"] = sha1;
    dic[@"params"] = aesStr;
    if (![strContent isEmpty]) {
        dic[@"content"] = strContent;
    }
    
    //6. 发起网络请求
    //   定义返回成功的block
    void (^requestSuccessedResult)(NSURLSessionDataTask *task, id responseObject) = ^(NSURLSessionDataTask *task, id responseObject) {
        if (isShowLoading) {
            [self hideLoading];
        }
        NSLog(@"url = %@  success! \n operation = %@ \n responseObject = %@",urlString,task, responseObject);
        if (isNeedModel) {
            JSONModelError *initError;
            BaseModel *baseModel = nil;
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                baseModel = [[BaseModel alloc] initWithDictionary:responseObject error:&initError];
            }
            else if ([responseObject isKindOfClass:[NSString class]]) {
                baseModel = [[BaseModel alloc] initWithString:responseObject error:&initError];
            }
            
            if (baseModel) {
                if (baseModel.resultCode == 400) {
                    requestFailure(baseModel.resultCode,baseModel.msg);
                } else {
                    
                    requestSuccessed(@"请求成功",baseModel);
                    
                }
            } else {
                if (initError) {
                    requestFailure(1001, initError.localizedDescription);
                } else {
                    requestFailure(1002, @"本地对象映射出错！");
                }
            }
        } else {
            if ([responseObject[@"resultCode"] integerValue ] == 200) {
                requestSuccessed([[responseObject[@"msg"] string]isEmpty]? @"请求成功":[responseObject[@"msg"] string],responseObject[@"resultCode"]);
            } else {
                requestFailure([responseObject[@"resultCode"] integerValue], [responseObject[@"msg"] string]);
            }
        }
    };
    //   定义返回失败的block
    void (^requestFailureResult)(NSURLSessionDataTask *task, NSError *error ) = ^(NSURLSessionDataTask *task, NSError *error) {
        
        if (isShowLoading) {
            [self hideLoading];
        }
        NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode ;
        NSLog(@"url = %@ \n failure! \n operation = %@ \n statusCode = %ld \n error = %@ ",urlString,task,(long)statusCode,error);
        if (appStore) {
            requestFailure(405,@"网络错误 , 请检查网络或关闭应用后重新再打开");
        } else {
            requestFailure(statusCode,[error description]);
        }
    };
    
    //  定义downloadProgress
    void (^downloadProgress)(NSProgress *downloadProgress)  = ^(NSProgress *downloadProgress) {
        
    };
    
    void (^uploadProgress)(NSProgress *uploadProgress) = ^(NSProgress *uploadProgress) {
        
    };
    
    NSLog(@"requestType = %ld, dictParam = %@", (long)requestType, dictParam);
    
    if (requestType == RequestTypeGET) {
        NSLog(@"getting data...");
        [manager GET:newUrlString parameters:dic progress:downloadProgress success:requestSuccessedResult failure:requestFailureResult];
    }
    
    else if (requestType == RequestTypePOST) {
        NSLog(@"posting data...");
        [manager POST:newUrlString parameters:dic progress:uploadProgress success:requestSuccessedResult failure:requestFailureResult];
    }
    else if (requestType == RequestTypeUploadFile) {
        NSLog(@"uploading data...");
        [manager POST:newUrlString parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            if ([dictParam[@"type"] isEqualToString:@"2"]) {
                // 图片
                [formData appendPartWithFileData:imageData name:@"file" fileName:@"img.png" mimeType:@""];
            } else if ([dictParam[@"type"] isEqualToString:@"1"]) {
                // 视频
                [formData appendPartWithFileData:imageData name:@"file" fileName:@"img.mp4" mimeType:@""];
            }
        } progress:uploadProgress success:requestSuccessedResult failure:requestFailureResult];
    }
    else if (requestType == RequestTypePostBodyData) {
        NSLog(@"posting bodydata...");
        NSMutableURLRequest *mutableRequest = [manager.requestSerializer requestWithMethod:@"POST" URLString:newUrlString parameters:nil error:nil];
        mutableRequest.HTTPBody = [bodyParam dataUsingEncoding:NSUTF8StringEncoding];
        __block NSURLSessionDataTask *dataTask = nil;
        NSURLSessionTask *tast = [manager dataTaskWithRequest:mutableRequest completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
            if (error) {
                if (requestFailureResult) {
                    requestFailureResult(dataTask, error);
                }
            } else {
                if (requestSuccessedResult) {
                    requestSuccessedResult(dataTask, responseObject);
                }
            }
        }];
        [tast resume];
        
    }

}

@end
